<template>
    <n-form class="w-[340px]" ref="formRef" :model="form" :rules="rules" :show-label="false">
        <n-form-item path="phone">
            <n-input v-model:value="form.phone" placeholder="手机号" />
        </n-form-item>

        <n-form-item path="code">
            <n-input-group>
                <n-input v-model:value="form.code" :style="{ width: '75%' }" placeholder="验证码" />
                <client-only>
                    <SendCode :phone="form.phone" />
                </client-only>
            </n-input-group>
        </n-form-item>



        <div class="">
            <n-button class="w-full" type="primary" @click="onSubmit" :loading="loading">
                绑 定
            </n-button>
        </div>

    </n-form>
</template>

<script setup>
// 引入CompositionAPI
import { ref, reactive, computed } from "vue";

// 引入 naive-ui 组件
import { NButton, NInput, NForm, NFormItem, createDiscreteApi, NInputGroup } from "naive-ui";

// 路由实例
const route = useRoute();

// formRef 实例
const formRef = ref();

// 标题
const title = ref("绑定手机号");

// 加载中
const loading = ref(false);

// 响应式数据
const form = reactive({
    phone: "",
    code: "",
});

// 规则
const rules = {
    phone: [
        {
            required: true,
            message: "手机号"
        },
    ],
    code: [
        {
            required: true,
            message: '验证码'
        }
    ]
}

// 提交表单
const onSubmit = () => {
    formRef.value.validate(async (error) => {
        if (!error) {

            loading.value = true;

            const { error, data } = await useBindPhoneApi(form);

            loading.value = false;

            if (error.value) return

            const { message } = createDiscreteApi(["message"]);

            message.success("绑定成功");

            // 跳转
            navigateTo(route.query.from || "/", { replace: true })

        };
    })
}

// 回车事件
useEnterEvent(() => onSubmit());

// 导航栏标题
useHead({ title: "绑定抖音号" })

// 为你的页面组件定义元数据。
definePageMeta({
    title: "绑定抖音号",
    layout: "login"
});
</script>
